// 引入ajax
import wxAjax from './../../utils/ajax.js';

// 微信小程序中虽然支持Promise，但是并不支持async和await
// 可以引入下面的包解决这个问题
import regeneratorRuntime from "./../../utils/runtime.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList: [
      {
        "id": 1,
        "image": "http://ww1.sinaimg.cn/mw690/006ThXL5ly1fj7zx3w751j30u00dmgy3.jpg",
        "link": ""
      },
      {
        "id": 2,
        "image": "http://ww1.sinaimg.cn/mw690/006ThXL5ly1fj6ckx9tlwj30u00fqk8n.jpg",
        "link": "/pages/list/list?cat=10"
      }
    ], // 轮播图数据
    categoryList: [{
      "id": 1,
      "name": "美食",
      "icon": "../../assets/images/food.jpg"
    },
    {
      "id": 2,
      "name": "景点",
      "icon": "../../assets/images/travel.jpg"
    },
    {
      "id": 3,
      "name": "娱乐",
      "icon": "../../assets/images/ktv.jpg"
    },
    {
      "id": 4,
      "name": "医院",
      "icon": "../../assets/images/hospital.jpg"
    },
    {
      "id": 5,
      "name": "药店",
      "icon": "../../assets/images/med.jpg"
    },
    {
      "id": 6,
      "name": "学校",
      "icon": "../../assets/images/school.jpg"
    },
    {
      "id": 7,
      "name": "热点资讯",
      "icon": "../../assets/images/news.png"
    },
    {
      "id": 8,
      "name": "特产",
      "icon": "../../assets/images/techan.jpg"
    },
    {
      "id": 9,
      "name": "好货",
      "icon": "../../assets/images/store.jpg"
    }
    ] // 分类数据
  },

  onLoad() {
    wx.showLoading({
      title: '加载中',
      mask: true
    });
  },

  onShow() {

    // 发送请求获取轮播图数据
    this.getSwiperList();

    // 发送请求获取分类数据
    this.getCategoryList();

  },

  // 发送请求获取轮播图数据
  async getSwiperList() {

    // let res = await wxAjax('/slides');
    // if (res.statusCode === 200) {
    //   this.data.swiperList = res.data;
    //   this.setData(this.data);
    //   wx.hideLoading();
    // }
    wx.hideLoading();
  },

  // 发送请求获取分类数据
  async getCategoryList() {
    wx.hideLoading();

    // let res = await wxAjax('/categories');
    // if (res.statusCode === 200) {
    //   this.data.categoryList = res.data;
    //   this.setData(this.data);
    //   wx.hideLoading();
    // }
  },

  // 跳转到列表页，将分类id传过去
  jumpToList(e) {
    let cid = e.currentTarget.dataset.cid;
    let cname = e.currentTarget.dataset.cname;
    wx.navigateTo({
      url: `/pages/list/list?cid=${cid}&cname=${cname}`
    });
  }
})